package com.alibaba.base.util;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *這是一個可以將响应数据转换成json数据
 * @author DP
 * 当前类是抽象方法，所有不可用new
 *
 */
public abstract class JsonUtil {
	
	private static final String SUCCCESS_CODE="200";
	private static final String ERROR_CODE="400";
	//构造方法私有化，当前类不可用被继承
	private JsonUtil() {}
	
	public static String success() {
		Map<String,String> map=new HashMap<String,String>(4);
		map.put("code",SUCCCESS_CODE);
		//将数组转成json
		//JSONArray.fromObject(XXX);
		return JSONObject.fromObject(map).toString();
	}
	public static String success(Object object) {
		Map<String,Object> map=new HashMap<String,Object>(4);
		map.put("code",SUCCCESS_CODE);
		map.put("data", object);
		return JSONObject.fromObject(map).toString();
	}
	
	public static String error(String msg) {
		Map<String,String> map=new HashMap<String,String>(4);
		map.put("code",ERROR_CODE);
		map.put("errormsg", msg);
		return JSONObject.fromObject(map).toString();
		
	}
 
}
